<?php
namespace App\Handler\Admin\Member\Member;

if (!defined('IN_PX'))
    exit;

use App\Repository;
use App\Handler\Admin\AbstractCommon;
use App\Tools\Auxi;
use App\Tools\Html;
use Phoenix\Support\MsgHelper;

/**
 * 添加
 */
class Add extends AbstractCommon
{
    /**
     * @param array $context
     */
    public function processRequest(Array & $context)
    {
        try{
            $this->db->beginTransaction();
            $this->_pushSetting();

            $exist = $this->db->table('`#@__@member`')->where('`member_name` = ? || `mobile` = ?')->bind(array($_POST['member_name'], $_POST['mobile']))->exists();
            if ($exist) {
                echo(MsgHelper::json('USER_IS_EXISTS', '用户已存在！'));
                exit;
            }

            $_POST['member_name'] = isset($_POST['member_name']) ? Html::getTextToHtml($_POST['member_name']) : '';
            $_POST['portrait'] = isset($_POST['portrait']) ? Html::getTextToHtml($_POST['portrait']) : '';
            $_POST['location'] = isset($_POST['location']) ? Html::getTextToHtml($_POST['location']) : '';
            $_POST['remarks'] = isset($_POST['remarks']) ? Html::getTextToHtml($_POST['remarks']) : '';
            $_POST['department_id'] = isset($_POST['department_id']) ? intval($_POST['department_id']) : 0;
            $_POST['is_status'] = isset($_POST['is_status']) ? intval($_POST['is_status']) : 0;
            $_POST['sex'] = isset($_POST['sex']) ?  intval($_POST['sex']) : 0;

            $_POST['password'] = isset($_POST['password']) ?  $_POST['password'] : '123456';
            $_POST['guid'] = Auxi::guid();
            $_POST['password'] = md5($_POST['password'] . $_POST['guid']);

            $_POST['add_date']   = time();
            $_POST['release_date']   = $_POST['add_date'];
            $_POST['last_log_date']   = $_POST['add_date'];
            $_POST['master_id'] = $this->session->adminUser['id'];

            $array = array(
                'mobile' => '?',
                'member_name' => '?',
                'is_status' => '?',
                'portrait' => '?',
                'location' => '?',
                'master_id' => '?',
                'update_time' => '?',
                'reg_time' => '?',
                'password' => '?',
                'guid' => '?',
                'remarks' => '?',
            );
            $_return = $this->db->table('`#@__@member`')
                ->row($array)
                ->bind($_POST)
                ->save();

            $this->db->commit();
            echo(MsgHelper::json(isset($_return) && $_return > 0 ? 'SUCCESS' : 'DB_ERROR'));
        }catch (\Exception $e) {
            $this->db->rollBack();
            return MsgHelper::json('DB_ERROR');
        }
    }

}
